<?php

/**
 * This is the model class for table "USER_DEF".
 *
 * The followings are the available columns in table 'USER_DEF':
 * @property string $USER_ID
 * @property integer $ACCT_ID
 * @property integer $CUST_ID
 * @property string $CREATED_BY
 * @property string $CREATION_DATE
 * @property string $PIN
 * @property double $BALANCE
 * @property double $BALANCE_REF
 * @property integer $PLAY_TYPE
 * @property integer $PLAYED_FLAG
 * @property integer $ACCT_HIGH
 * @property integer $STATUS
 * @property string $ACT_DATE
 * @property integer $TTL
 * @property integer $RESELLER_ID
 * @property integer $ERR_COUNTER
 * @property integer $LIMIT
 * @property string $PREPAID_CARD_SEQ
 * @property string $REMARK
 * @property string $BILLED_DATE
 * @property integer $FEEPLAN_ID
 * @property integer $MAX_ONLINE_CALLS
 * @property integer $ONLINE_CALLS
 * @property integer $TIME_USAGE_INT
 * @property integer $TIME_USAGE_DOM
 * @property integer $TIME_USAGE_MOB
 * @property integer $TIME_USAGE_LOC
 * @property integer $MISCFEATURE_FLAG
 * @property integer $CALL_COUNTER
 * @property integer $MISCFEATURE_ID_1
 * @property integer $MISCFEATURE_ID_2
 * @property integer $MISCFEATURE_ID_3
 * @property string $USER_NAME
 * @property string $PASSWORD
 * @property string $ALT_ID
 * @property double $OUTSTANDING_BALANCE
 * @property integer $ACCESS_CODE_ALWAYS
 * @property integer $APPFLAG
 * @property double $FACE_VALUE
 * @property integer $CARD_BATCH_ID
 * @property string $FIRST_CALL_DATE
 * @property string $LAST_CALL_DATE
 * @property string $CARD_NUMBER
 * @property string $AVAILABLE_DATE
 *
 * The followings are the available model relations:
 * @property ACCOUNT $aCCT
 * @property FEEPLANDESC $fEEPLAN
 * @property CUSTOMER $rESELLER
 * @property SUBSCRIBEDFEATURES[] $sUBSCRIBEDFEATURESs
 * @property REJECTEDTRANSACTIONLOCAL[] $rEJECTEDTRANSACTIONLOCALs
 * @property PURCHASEORDER[] $pURCHASEORDERs
 * @property MERCHANTTRANSACTIONLOG[] $mERCHANTTRANSACTIONLOGs
 * @property EPTOUSERDEF[] $ePTOUSERDEFs
 * @property CREDITCARD[] $cREDITCARDs
 * @property CONTACT[] $cONTACTs
 * @property CHARGE[] $cHARGEs
 * @property ACCTACCESSCODE[] $aCCTACCESSCODEs
 */
class USERDEF extends Post3ActiveRecord
{

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return USERDEF the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'POST3.USER_DEF';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ACCT_ID, CUST_ID, BALANCE, STATUS, ACT_DATE, PREPAID_CARD_SEQ', 'required'),
			array('ACCT_ID, CUST_ID, PLAY_TYPE, PLAYED_FLAG, ACCT_HIGH, STATUS, TTL, RESELLER_ID, ERR_COUNTER, LIMIT, FEEPLAN_ID, MAX_ONLINE_CALLS, ONLINE_CALLS, TIME_USAGE_INT, TIME_USAGE_DOM, TIME_USAGE_MOB, TIME_USAGE_LOC, MISCFEATURE_FLAG, CALL_COUNTER, MISCFEATURE_ID_1, MISCFEATURE_ID_2, MISCFEATURE_ID_3, ACCESS_CODE_ALWAYS, APPFLAG, CARD_BATCH_ID', 'numerical', 'integerOnly'=>true),
			array('BALANCE, BALANCE_REF, OUTSTANDING_BALANCE, FACE_VALUE', 'numerical'),
			array('CREATED_BY', 'length', 'max'=>20),
			array('PIN', 'length', 'max'=>10),
			array('PREPAID_CARD_SEQ, CARD_NUMBER', 'length', 'max'=>24),
			array('REMARK', 'length', 'max'=>255),
			array('USER_NAME, PASSWORD, ALT_ID', 'length', 'max'=>64),
			array('CREATION_DATE, BILLED_DATE, FIRST_CALL_DATE, LAST_CALL_DATE, AVAILABLE_DATE', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('USER_ID, ACCT_ID, CUST_ID, CREATED_BY, CREATION_DATE, PIN, BALANCE, BALANCE_REF, PLAY_TYPE, PLAYED_FLAG, ACCT_HIGH, STATUS, ACT_DATE, TTL, RESELLER_ID, ERR_COUNTER, LIMIT, PREPAID_CARD_SEQ, REMARK, BILLED_DATE, FEEPLAN_ID, MAX_ONLINE_CALLS, ONLINE_CALLS, TIME_USAGE_INT, TIME_USAGE_DOM, TIME_USAGE_MOB, TIME_USAGE_LOC, MISCFEATURE_FLAG, CALL_COUNTER, MISCFEATURE_ID_1, MISCFEATURE_ID_2, MISCFEATURE_ID_3, USER_NAME, PASSWORD, ALT_ID, OUTSTANDING_BALANCE, ACCESS_CODE_ALWAYS, APPFLAG, FACE_VALUE, CARD_BATCH_ID, FIRST_CALL_DATE, LAST_CALL_DATE, CARD_NUMBER, AVAILABLE_DATE', 'safe', 'on'=>'search'),
		);
	}
	

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'aCCT' => array(self::BELONGS_TO, 'ACCOUNT', 'ACCT_ID'),
			'fEEPLAN' => array(self::BELONGS_TO, 'FEEPLANDESC', 'FEEPLAN_ID'),
			'rESELLER' => array(self::BELONGS_TO, 'CUSTOMER', 'RESELLER_ID'),
			'sUBSCRIBEDFEATURESs' => array(self::HAS_MANY, 'SUBSCRIBEDFEATURES', 'PPR_USER_ID'),
			'rEJECTEDTRANSACTIONLOCALs' => array(self::HAS_MANY, 'REJECTEDTRANSACTIONLOCAL', 'SUBSCRIBER_ID'),
			'pURCHASEORDERs' => array(self::HAS_MANY, 'PURCHASEORDER', 'SUBSCRIBER_ID'),
			'mERCHANTTRANSACTIONLOGs' => array(self::HAS_MANY, 'MERCHANTTRANSACTIONLOG', 'SUBSCRIBER_ID'),
			'ePTOUSERDEFs' => array(self::HAS_MANY, 'EPTOUSERDEF', 'PPR_USER_ID'),
			'cREDITCARDs' => array(self::HAS_MANY, 'CREDITCARD', 'SUBSCRIBER_ID'),
			'cONTACTs' => array(self::HAS_MANY, 'CONTACT', 'PPR_USER_ID'),
			'cHARGEs' => array(self::HAS_MANY, 'CHARGE', 'PPR_USER_ID'),
			'aCCTACCESSCODEs' => array(self::HAS_MANY, 'ACCTACCESSCODE', 'SUBSCRIBER_ID'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'USER_ID' => 'User',
			'ACCT_ID' => 'Acct',
			'CUST_ID' => 'Cust',
			'CREATED_BY' => 'Created By',
			'CREATION_DATE' => 'Creation Date',
			'PIN' => 'Pin',
			'BALANCE' => 'Balance',
			'BALANCE_REF' => 'Balance Ref',
			'PLAY_TYPE' => 'Play Type',
			'PLAYED_FLAG' => 'Played Flag',
			'ACCT_HIGH' => 'Acct High',
			'STATUS' => 'Status',
			'ACT_DATE' => 'Act Date',
			'TTL' => 'Ttl',
			'RESELLER_ID' => 'Reseller',
			'ERR_COUNTER' => 'Err Counter',
			'LIMIT' => 'Limit',
			'PREPAID_CARD_SEQ' => 'Prepaid Card Seq',
			'REMARK' => 'Remark',
			'BILLED_DATE' => 'Billed Date',
			'FEEPLAN_ID' => 'Feeplan',
			'MAX_ONLINE_CALLS' => 'Max Online Calls',
			'ONLINE_CALLS' => 'Online Calls',
			'TIME_USAGE_INT' => 'Time Usage Int',
			'TIME_USAGE_DOM' => 'Time Usage Dom',
			'TIME_USAGE_MOB' => 'Time Usage Mob',
			'TIME_USAGE_LOC' => 'Time Usage Loc',
			'MISCFEATURE_FLAG' => 'Miscfeature Flag',
			'CALL_COUNTER' => 'Call Counter',
			'MISCFEATURE_ID_1' => 'Miscfeature Id 1',
			'MISCFEATURE_ID_2' => 'Miscfeature Id 2',
			'MISCFEATURE_ID_3' => 'Miscfeature Id 3',
			'USER_NAME' => 'User Name',
			'PASSWORD' => 'Password',
			'ALT_ID' => 'Alt',
			'OUTSTANDING_BALANCE' => 'Outstanding Balance',
			'ACCESS_CODE_ALWAYS' => 'Access Code Always',
			'APPFLAG' => 'Appflag',
			'FACE_VALUE' => 'Face Value',
			'CARD_BATCH_ID' => 'Card Batch',
			'FIRST_CALL_DATE' => 'First Call Date',
			'LAST_CALL_DATE' => 'Last Call Date',
			'CARD_NUMBER' => 'Card Number',
			'AVAILABLE_DATE' => 'Available Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('USER_ID',$this->USER_ID,true);
		$criteria->compare('ACCT_ID',$this->ACCT_ID);
		$criteria->compare('CUST_ID',$this->CUST_ID);
		$criteria->compare('CREATED_BY',$this->CREATED_BY,true);
		$criteria->compare('CREATION_DATE',$this->CREATION_DATE,true);
		$criteria->compare('PIN',$this->PIN,true);
		$criteria->compare('BALANCE',$this->BALANCE);
		$criteria->compare('BALANCE_REF',$this->BALANCE_REF);
		$criteria->compare('PLAY_TYPE',$this->PLAY_TYPE);
		$criteria->compare('PLAYED_FLAG',$this->PLAYED_FLAG);
		$criteria->compare('ACCT_HIGH',$this->ACCT_HIGH);
		$criteria->compare('STATUS',$this->STATUS);
		$criteria->compare('ACT_DATE',$this->ACT_DATE,true);
		$criteria->compare('TTL',$this->TTL);
		$criteria->compare('RESELLER_ID',$this->RESELLER_ID);
		$criteria->compare('ERR_COUNTER',$this->ERR_COUNTER);
		$criteria->compare('LIMIT',$this->LIMIT);
		$criteria->compare('PREPAID_CARD_SEQ',$this->PREPAID_CARD_SEQ,true);
		$criteria->compare('REMARK',$this->REMARK,true);
		$criteria->compare('BILLED_DATE',$this->BILLED_DATE,true);
		$criteria->compare('FEEPLAN_ID',$this->FEEPLAN_ID);
		$criteria->compare('MAX_ONLINE_CALLS',$this->MAX_ONLINE_CALLS);
		$criteria->compare('ONLINE_CALLS',$this->ONLINE_CALLS);
		$criteria->compare('TIME_USAGE_INT',$this->TIME_USAGE_INT);
		$criteria->compare('TIME_USAGE_DOM',$this->TIME_USAGE_DOM);
		$criteria->compare('TIME_USAGE_MOB',$this->TIME_USAGE_MOB);
		$criteria->compare('TIME_USAGE_LOC',$this->TIME_USAGE_LOC);
		$criteria->compare('MISCFEATURE_FLAG',$this->MISCFEATURE_FLAG);
		$criteria->compare('CALL_COUNTER',$this->CALL_COUNTER);
		$criteria->compare('MISCFEATURE_ID_1',$this->MISCFEATURE_ID_1);
		$criteria->compare('MISCFEATURE_ID_2',$this->MISCFEATURE_ID_2);
		$criteria->compare('MISCFEATURE_ID_3',$this->MISCFEATURE_ID_3);
		$criteria->compare('USER_NAME',$this->USER_NAME,true);
		$criteria->compare('PASSWORD',$this->PASSWORD,true);
		$criteria->compare('ALT_ID',$this->ALT_ID,true);
		$criteria->compare('OUTSTANDING_BALANCE',$this->OUTSTANDING_BALANCE);
		$criteria->compare('ACCESS_CODE_ALWAYS',$this->ACCESS_CODE_ALWAYS);
		$criteria->compare('APPFLAG',$this->APPFLAG);
		$criteria->compare('FACE_VALUE',$this->FACE_VALUE);
		$criteria->compare('CARD_BATCH_ID',$this->CARD_BATCH_ID);
		$criteria->compare('FIRST_CALL_DATE',$this->FIRST_CALL_DATE,true);
		$criteria->compare('LAST_CALL_DATE',$this->LAST_CALL_DATE,true);
		$criteria->compare('CARD_NUMBER',$this->CARD_NUMBER,true);
		$criteria->compare('AVAILABLE_DATE',$this->AVAILABLE_DATE,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * Add ani subscrbier by web services 
	 */
	public static function addAniSubscriber($first_name, $last_name, $phones)
	{
		$service  = new SoapClient("http://208.74.72.210:8080/EnticeWS/services/billing3?wsdl");
		
		$xml = new SimpleXMLElement('<EnticeRequest></EnticeRequest>');
		$xmldata = $xml->addChild('data');
			$row = $xmldata->addChild('row');
				$row->addChild('ACCT_ID',CGlobal::$wsAccountID);
				$row->addChild('CUST_ID',CGlobal::$wsCustID);
				$row->addChild('FIRST_NAME',$first_name);
				$row->addChild('LAST_NAME',$last_name);
				$row->addChild('LIMIT',0);
				$row->addChild('USER_NAME','');
				$row->addChild('PASSWORD','');
				$row->addChild('STATUS','6');
				$row->addChild('TTL',NULL);
					$contact = $row->addChild('ANILIST');
					foreach($phones as $index => $value)
					{
						$ani = $contact->addChild('ANI');
						$ani->addChild('NUMBER',$value);
						$ani->addChild('PIN',CGlobal::$wsPinDefault);
					}
					
		$data = $service->addANISubscriber(CGlobal::$wsResellerId,CGlobal::getResllerKey(),$xml->asXML(),'bao');
		
		return $data;
	}
	
	/**
	 * 
	 */
	public static function addPhone($phone)
	{
				
	}
}